var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        roomlist: [],
        form: {
            startTime: '',
            endTime: '',
            activeDate: 0,
            ChargeMoneyTypes: [],
            ChargeIDList: [],
            ProjectIDs: [],
            activeDateDesc: '时间',
            ProjectCompanyType: 1,
            CompanyIDs: []
        },
        chargeMoneyTypeList: [],
        chargeSummaryList: [],
        projectList: [],
        companyList: [],
        projectCompanyList: [{
            ID: 1,
            Name: '按项目'
        }, {
            ID: 2,
            Name: '按公司'
        }],
        summary: {
            TotalCost: '',
            totalRoomCount: '',
            TotalBackCost:'',
            totalBackRoomCount:'',
            category_list: [],
            return_type_list:[],
            projectFeeList: [],
        },
        type_list:[],
        chargecategory_list:[],
        openIDList:[],
        // isOpenJD:false,
    },
    methods: {
        get_data: function() {
            var that = this;
            that.getSearchParams();
            setTimeout(function() {
                that.getAnalysisData();
            }, 1000)
        },
        doSelectCompany: function(ID) {
            var that = this;
            var index = that.form.CompanyIDs.indexOf(ID);
            if (index > -1) {
                that.form.CompanyIDs.splice(index, 1);
            } else {
                that.form.CompanyIDs.push(ID);
            }
        },
        // do_openJD:function(){
        //     var that=this;
        //     that.isOpenJD=!that.isOpenJD;
        // },
        openroom: function(item){
          // alert(JSON.stringify(item));
          var index=this.openIDList.indexOf(item.ID);
          if(index>-1){
            this.openIDList.splice(index,1);
          }else{
            this.openIDList.push(item.ID)
          }
        },
        doSelectProjectCompany: function(ID) {
            this.form.ProjectCompanyType = ID;
        },
        doSelectChargeMoneyType: function(ID) {
            var that = this;
            var index = that.form.ChargeMoneyTypes.indexOf(ID);
            if (index > -1) {
                that.form.ChargeMoneyTypes.splice(index, 1);
            } else {
                that.form.ChargeMoneyTypes.push(ID);
            }
        },
        doSelectChargeCategory: function(ID) {
            var that = this;
            var index = that.form.ChargeIDList.indexOf(ID);
            if (index > -1) {
                that.form.ChargeIDList.splice(index, 1);
            } else {
                that.form.ChargeIDList.push(ID);
            }
        },
        doSelectProject: function(ID) {
            var that = this;
            var index = that.form.ProjectIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectIDs.splice(index, 1);
            } else {
                that.form.ProjectIDs.push(ID);
            }
        },
        //筛选数据
        getSearchParams: function() {
            var that = this;
            var options = {};
            options.action = "getanalysissearchparam";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.chargeMoneyTypeList = data.chargeMoneyTypeList;
                    that.chargeSummaryList = data.chargeSummaryList;
                    that.projectList = data.projectList;
                    that.companyList = data.companyList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getAnalysisData: function() {
            var that = this;
            var options = {};
            var ProjectIDs = [];
            var CompanyIDs = [];
            if (that.form.ProjectCompanyType == 1) {
                ProjectIDs = that.form.ProjectIDs;
            } else {
                CompanyIDs = that.form.CompanyIDs;
            }
            options.action = "getanalysisfeehistorysummary";
            options.ChargeIDList = JSON.stringify(that.form.ChargeIDList);
            options.ChargeMoneyTypes = JSON.stringify(that.form.ChargeMoneyTypes);
            options.ProjectIDs = JSON.stringify(ProjectIDs);
            options.CompanyIDs = JSON.stringify(CompanyIDs);
            options.StartChargeTime = that.form.startTime;
            options.EndChargeTime = that.form.endTime;
            options.ProjectCompanyType = that.form.ProjectCompanyType;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.summary = data;
                    that.type_list=data.type_list;
                    that.chargecategory_list=data.chargecategory_list;
                  
                    if(that.chargecategory_list.length>0){
                        setTimeout(function () {
                            that.loadZZChart(data.chargecategory_list);
                        }, 100);
                        
                    }
                    if(that.type_list.length>0){
                        setTimeout(function () {
                            that.loadChart(data.type_list);
                        }, 100);
                    }
                   
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        doSelectSearchParam: function(type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function() {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            if (dataValue == 3) {
                var currentYear = dateRangeUtil.getCurrentYear();
                startDate = dateRangeUtil.getDateString(currentYear[0]);
                endDate = dateRangeUtil.getDateString(currentYear[1]);
                that.form.activeDateDesc = '本年';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        doReset: function(type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc='时间';
            } else {
                that.form.ChargeIDList = [];
                that.form.ChargeMoneyTypes = [];
                that.form.ProjectIDs = [];
            }
        },
        doSearch: function() {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        loadZZChart:function(zzlist){
            Highcharts.chart('zzcontainer', {
                chart: {
                    type: 'column'
                },
                credits: {
                    enabled: false //不显示LOGO
                },
                exporting: {
                    enabled:false
                },
                title: {
                    text: ''
                },
                subtitle: {
                    text: ''
                },
                xAxis: {
                    type: 'category'
                },
                yAxis: {
                    title: {
                        text: ''
                    }
                },
                legend: {
                    enabled: false
                },
                plotOptions: {
                    series: {
                        borderWidth: 0,
                        dataLabels: {
                            enabled: false,
                            // format: '{point.y:.1f}%'
                        }
                    }
                },
                tooltip: {
                    // pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                series: [{
                    name: '金额',
                    colorByPoint: false,
                    color:"#289fff",
                    data:zzlist
                }],
            });
        },
        loadChart: function(list) {
            var chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container',
                    spacing: [0, 0, 0, 0],
                    panning: false,
                },
                colors: ['#289fff', '#9B88ED', '#77D2A9', '#0E69B3',
				 '#FFAB29', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'],
                
                credits: {
                    enabled: false //不显示LOGO
                },
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    text: '',
                },
                subtitle: {
                    text: '',
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                legend:{
                    align:'center',
                    labelFormat: '<span style="{color}">{name} {y}</span>',
                    itemStyle: { 
                       color: '#3E576F' ,
                       fontWeight:'400',
                       fontSize:'12px',
                    }
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false,
                            // format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            // style: {
                            //     color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            // }
                        },
                        showInLegend: true
                    }
                },
                series: [{
                    type: 'pie',
                    // innerSize: '70%',
                    name: '占比',
                    data: list
                }]
            }, function(c) { // 图表初始化完毕后的会掉函数
            });
        },
        do_select_time: function(type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function(ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_data();
    app.doClickDay(2);
};
